package com.gem.talkbase.adapter;



import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.tsz.afinal.FinalBitmap;

import com.gem.talkbase.R;
import com.gem.talkbase.activity.Chat_Activity;
import com.gem.talkbase.bean.ChatMsg;
import com.gem.talkbase.client.Client;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



 public  class ChatMsg_Adapter extends BaseAdapter {
		private List<ChatMsg> coll ;// 消息对象数组
		private Context cxt;
		private FinalBitmap fb ;
		private LayoutInflater inflater;
		private String friendname,headimage;
		
		
	
		public ChatMsg_Adapter(Chat_Activity formClient, List<ChatMsg> coll,String friendname,String headimage) {
			 this.cxt = formClient;
			 this.coll=coll;
			coll=new ArrayList<ChatMsg>();
			Iterator<ChatMsg> iterator=coll.iterator();
			while (iterator.hasNext()) {
				ChatMsg chatMsg =new ChatMsg();
				chatMsg=iterator.next();
				System.out.println(chatMsg.getFrom()+"==="+chatMsg.getMsg()+"==="+chatMsg.getFrom()+"===="+chatMsg.getDate());
					
			}
			
			this.friendname = friendname;
			this.headimage = headimage;
		}
		

		@Override
		public int getCount() {
			return coll.size();
		}

		@Override
		public Object getItem(int position) {
			return coll.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			
			fb = FinalBitmap.create(cxt);
			this.inflater = (LayoutInflater) this.cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			ChatMsg entity = coll.get(position);

			ViewHolder viewHolder = null;
//			if (convertView == null) {
			
				if ("IN".equals(entity.from)) {
					convertView = inflater.inflate(
							R.layout.chatting_item_msg_text_left, null);
				} else {
					convertView = inflater.inflate(
							R.layout.chatting_item_msg_text_right, null);
				}
			
				viewHolder = new ViewHolder();
				viewHolder.tvSendTime = (TextView) convertView
						.findViewById(R.id.tv_sendtime);
				viewHolder.tvUserName = (TextView) convertView
						.findViewById(R.id.tv_username);
				viewHolder.tvContent = (TextView) convertView
						.findViewById(R.id.tv_chatcontent);
				viewHolder.icon = (ImageView) convertView
						.findViewById(R.id.iv_userhead);
				viewHolder.icon1 = (ImageView) convertView
						.findViewById(R.id.iv_userhead1);
				convertView.setTag(viewHolder);  
			
			viewHolder.tvSendTime.setText(entity.getDate());
			if("IN".equals(entity.from)){
		//		viewHolder.tvUserName.setText(friendname);
				fb.display(viewHolder.icon1 ,headimage);
				
			}else{
		//		viewHolder.tvUserName.setText(Client.userName);
				fb.display(viewHolder.icon ,Client.userHeadimage);
			}
			viewHolder.tvContent.setText(entity.getMsg());
			return convertView;
		    
	  	}
		
		static class ViewHolder {
			public TextView tvSendTime;
			public TextView tvUserName;
			public TextView tvContent;
			public ImageView icon;
			public ImageView icon1;	
		}
   }

